package com.gzhryc.shared_device.oem.h5.mng.controllers.cdb;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.cdb.code.dao.db.BatteryCabinet;
import com.gzhryc.shared_device.cdb.code.dao.db.BatteryCabinetSlot;
import com.gzhryc.shared_device.oem.code.device.CDBCacheManager;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 充电插座设备缓存
 */
public class BatteryCabinetCacheController {

    @Route(value = { "/cdb/device/cache/list.html" })
    public String list(HttpServletRequest request) {
        return "/cdb/device/cache/list.html";
    }

    @Route(value = "/cdb/device/cache/list.data")
    public ListResult<BatteryCabinet> listData(HttpServletRequest request) {
        ListResult<BatteryCabinet> result = new ListResult<>(DtoConstants.NULL_DATA,"暂无数据");
        List<BatteryCabinet> data = CDBCacheManager.self().findCDBDevice();
        if(data != null && data.size() > 0) {
            result.setCode(DtoConstants.SUCCESS,null);
            result.getBody().setData(data);
        }
        return result;
    }

    @Route(value = "/cdb/device/slot/cache/list.data")
    public ListResult<BatteryCabinetSlot> slotListData(HttpServletRequest request) {
        ListResult<BatteryCabinetSlot> result = new ListResult<>(DtoConstants.NULL_DATA,"暂无数据");
        List<BatteryCabinetSlot> data = CDBCacheManager.self().findCDBSlotDevice();
        if(data != null && data.size() > 0) {
            result.setCode(DtoConstants.SUCCESS,null);
            result.getBody().setData(data);
        }
        return result;
    }

    @Route(value = { "/cdb/device/cache/del.do" })
    public AjaxResult doDel(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "删除失败");
        String deviceSn = WebUtils.getString(request, "deviceSn");
        if (StringTools.isNotBlank(deviceSn)) {
            try {
                CDBCacheManager.self().removeBatteryCabinet(deviceSn);
                result.setCode(DtoConstants.SUCCESS, null);
            }catch (Exception e){
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }

    @Route(value = { "/cdb/device/slot/cache/del.do" })
    public AjaxResult doSlotDel(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "删除失败");
        String deviceSn = WebUtils.getString(request, "deviceSn");
        Integer slotNum = WebUtils.getInteger(request, "slotNum");
        if (StringTools.isNotBlank(deviceSn) && slotNum != null) {
            try {
                CDBCacheManager.self().removeBatteryCabinetSlot(deviceSn,slotNum);
                result.setCode(DtoConstants.SUCCESS, null);
            }catch (Exception e){
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }
}
